using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


namespace Game1
{
    public class SubMenu
    {
        private string name;
        private List<MenuEntry> MenuEntries;
        
        private int index=0;
        private int cont;

        private bool active = true;
        private bool visible = false;

        private Color activeColor;
        private Color normalColor;

        private Vector2 offset;
        private SpriteBatch spriteBatch;

        public SubMenu(string name,int offsetX,int offsetY,Color activeColor,Color normalColor)
        {
            this.name = name;
            this.normalColor = normalColor;
            this.activeColor = activeColor;
            offset.X = offsetX;
            offset.Y = offsetY;
            spriteBatch = Base.getSpriteBatch();
            MenuEntries = new List<MenuEntry>();
        }

        public bool Active
        {
            get { return active; }
            set 
            {
                active = value;
                if (active == true)
                {
                    index = 0;
                    MenuEntries[index].changeColor(activeColor);
                }
                else
                {
                    MenuEntries[index].changeColor(normalColor);
                }
            }
        }

        public bool Visible
        {
            get { return visible; }
            set { visible = value; }
        }

        public string Name
        {
            get { return name; }
        }

        public void selectUp()
        {
            if (active)
            {
                MenuEntries[index].changeColor(normalColor);
                index++;
                if (index >= MenuEntries.Count)
                    index = 0;
                MenuEntries[index].changeColor(activeColor); 
            }
        }

        public void selectDown()
        {
            if (active)
            {
                MenuEntries[index].changeColor(normalColor);
                index--;
                if (index == -1) index = MenuEntries.Count - 1;
                MenuEntries[index].changeColor(activeColor); 
            }
        }
        public int getIndex(MenuEntry Entry)
        {
            for (cont=0;cont<= MenuEntries.Count;cont++)
            {
                if (MenuEntries[cont] == Entry) return cont;
            }
            return -1;
        }
        public void selectFromMouse()
        {
            foreach (MenuEntry Entry in MenuEntries)
            {
                if (Entry.verifyMouseInSquare())
                {
                    if (active)
                    {
                        MenuEntries[index].changeColor(normalColor);
                        index = getIndex(Entry);
                        if (index == -1) index = MenuEntries.Count - 1;
                        MenuEntries[index].changeColor(activeColor);
                    }
                }
            }
        }
        public MenuEntry getSelected()
        {
            return MenuEntries[index];
        }

        public void AddMenuEntry(MenuEntry newEntry)
        {
            MenuEntries.Add(newEntry);
        }
        public void AddMenuEntry(string Name, string Caption, int X, int Y, float deltax, float deltay)
        {
            MenuEntries.Add(new MenuEntry(Name, Caption, X + (int)offset.X, Y + (int)offset.Y, 1.0f, normalColor, deltax, deltay));
        }
        public void AddMenuEntry(string Name, string Caption, int X, int Y, float scale, float deltax, float deltay)
        {
            MenuEntries.Add(new MenuEntry(Name, Caption, X + (int)offset.X, Y + (int)offset.Y, scale, normalColor, deltax, deltay));
        }
        public void draw()
        {
            if (visible)
            {
                foreach (MenuEntry Entry in MenuEntries)
                {
                    //if(Entry.verifyMouseInSquare()) spriteBatch.Draw(Base.Content.Load<Texture2D>("Gui\\quadrado"), new Rectangle((int)Entry.getPosition().X, (int)Entry.getPosition().Y,((int)(Entry.getMouseSquare().xb - Entry.getMouseSquare().xa)), 25), Color.Yellow);
                    spriteBatch.DrawString(GuiManager.Singleton.Font, Entry.getCaption(), Entry.getPosition(), Entry.getColor(), 0.0f, Vector2.Zero, Entry.scale, SpriteEffects.None, 0.0f);
                }
            }
        }
        
    }
}
